package com.open.capacity.paper.controller;

import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.model.Paper;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.log.annotation.LogAnnotation;
import com.open.capacity.paper.service.PaperService;
import com.open.capacity.paper.vo.PaperVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author liangd
 * @since 2021-02-01 14:35
 */
@Api(tags = "问卷管理")
@RestController
@RequestMapping("/paper")
public class PaperController {

    @Autowired
    private PaperService paperService;

    /**
     * 模糊查询
     */
    @GetMapping("/getPapers")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置",  dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置",  dataType = "Integer")
    })
    @ApiOperation(value = "后台管理查询所有的问卷标识")
    @LogAnnotation(module = "paper-center")
    public PageResult<PaperVo> getPapers(@RequestParam Map<String, Object> params) throws ControllerException{
        try {
            return paperService.getPapers(params);
        }catch (Exception e){
            throw new ControllerException(e);
        }
    }

    /**
     * 新增或更新
     */
    @PostMapping("/saveOrUpdate")
    @LogAnnotation(module = "paper-center")
    @ApiOperation(value = "后台管理新增或者更新问卷标识,papeid==null?新增:更新")
    public Result saveOrUpdate(@RequestBody Paper paper) throws ControllerException{
        try {
            if (paper.getPapeid() != null) {
                return paperService.updatePaper(paper);
            } else {
                return paperService.savePaper(paper);
            }
        }catch (Exception e){
            throw new ControllerException(e);
        }
    }

    /**
     * 逻辑删除
     */
    @DeleteMapping("/delete/{papeid}")
    @LogAnnotation(module = "paper-center")
    @ApiOperation(value = "后台管理删除问卷标识")
    public Result delete(@PathVariable Integer papeid)throws ControllerException{
        try {
            return paperService.deletePaper(papeid);
        }catch (Exception e){
            throw new ControllerException(e);
        }
    }

}
